package com.mall.common.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.service.BackupService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
/**
 * 后台Action类 - 系统备份还原<相册备份、数据库备份>
 * @author wangzx
 */
@Controller
@RequestMapping("/admin/system/backup")
public class BackupController extends BaseController {
	@Resource BackupService backupService;
	
	/**
	 * 相册备份 列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/album", method=RequestMethod.GET)
	public String album(Model model) {
		return "/admin/system/backup/album";
	}
	
	@RequestMapping("/queryAlbumData")
	@ResponseBody
	public Pager queryAlbumData(Pager pager,HttpServletRequest request) {
		pager = backupService.getAlbums(pager);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	/**
	 * 备份 相册
	 * @param response
	 * @return
	 */
	@RequestMapping("/backupAlbum")
	@ResponseBody
	public String backupAlbum(HttpServletResponse response) {
		backupService.backupAlbum();
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	/**
	 * 还原 相册
	 * @param response
	 * @return
	 */
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "path", message = "名称不能为空")
		}
	)
	@RequestMapping("/restoreAlbum")
	@ResponseBody
	public String restoreAlbum(String path, HttpServletResponse response) {
		backupService.restoreAlbum(path);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	/**
	 * 相册备份 列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/db", method=RequestMethod.GET)
	public String db(Model model) {
		return "/admin/system/backup/db";
	}
	
	@RequestMapping("/queryDbData")
	@ResponseBody
	public Pager queryDbData(Pager pager,HttpServletRequest request) {
		pager = backupService.getDbs(pager);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	/**
	 * 备份 相册
	 * @param response
	 * @return
	 */
	@RequestMapping("/backupDb")
	@ResponseBody
	public String backupDb(HttpServletResponse response) {
		backupService.backupDb();
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	/**
	 * 还原 相册
	 * @param response
	 * @return
	 */
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "path", message = "名称不能为空")
		}
	)
	@RequestMapping("/restoreDb")
	@ResponseBody
	public String restoreDb(String path, HttpServletResponse response) {
		backupService.restoreDb(path);
		return ResponseUtils.renderJSON(successMessage, response);
	}
}
